package chapter10.eg13;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.util.CharsetUtil;

public abstract class AbstractHttpXmlEncoder<T> extends MessageToMessageEncoder<T> {
  XStream xstream = new XStream(new StaxDriver());

  protected ByteBuf encode0(ChannelHandlerContext ctx, Object body) {
    String xml = xstream.toXML(body);
    return Unpooled.copiedBuffer(xml, CharsetUtil.UTF_8);
  }
}
